/* Global defines */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* Project defines */
#include "main.h"
#include "trace.h"
#include "crc16.h"
#include "tools.h"
#include "xelix_proto.h"

int main()
{
    s_xelix_proto_t xelix_msg = {
    	.msg_header = XELIX_PROTO_HEADER,
        .msg_type = e_xelix_msg_data_push,
        .msg_sz = 0x0F,
        //.msg_sz = 0x01,
        .msg_content = malloc(0x0F),
        //.msg_content = malloc(0x01),
        .crc16 = 0x0
    };

    s_xelix_proto_t xelix_rx_msg;

    uint8_t buff[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
    //uint8_t buff[] = {1};

    int ret = ERR;
    uint8_t *msg_buffer = NULL;
	int msg_size;
    TRACE_MAIN("xelix test started!");

	srand (time(NULL));

    msg_buffer =malloc(XELIX_PROTO_STATIC+xelix_msg.msg_sz);
	memcpy((void*)xelix_msg.msg_content, (void*)buff, xelix_msg.msg_sz);
	msg_size = XELIX_PROTO_STATIC+xelix_msg.msg_sz;

    ret = xelix_msg_create(&xelix_msg, msg_buffer, &msg_size);
    if (ret <= 0)
        TRACE_ERROR("ret: %d", ret);

	ret = xelix_msg_parse(msg_buffer, &xelix_rx_msg, &ret);
	if (ret <= 0)
        TRACE_ERROR("ret: %d", ret);
	xelix_proto_struct_print(&xelix_rx_msg);
	free(xelix_rx_msg.msg_content);
    free(xelix_msg.msg_content);
    free(msg_buffer);
    return 0;
}
